package com.open.capacity.finance.util;


import com.open.capacity.finance.dto.KmyebDto;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.List;

public class ExcelKmyeb {
    private HSSFWorkbook workbook;
    private HSSFSheet sheet;

    /**
     * 创建行元素
     *
     * @param style  样式
     * @param height 行高
     * @param value  行显示的内容
     * @param row1   起始行
     * @param row2   结束行
     * @param col1   起始列
     * @param col2   结束列
     */
    private void createRow(HSSFCellStyle style, int height, String value, int row1, int row2, int col1, int col2) {

        sheet.addMergedRegion(new CellRangeAddress(row1, row2, col1, col2));  //设置从第row1行合并到第row2行，第col1列合并到col2列
        HSSFRow rows = sheet.createRow(row1);        //设置第几行
        rows.setHeight((short) height);              //设置行高
        HSSFCell cell = rows.createCell(col1);       //设置内容开始的列
        cell.setCellStyle(style);                    //设置样式
        cell.setCellValue(value);                    //设置该行的值
    }

    /**
     * 创建样式
     *
     * @param fontSize 字体大小
     * @param align    水平位置  左右居中2 居右3 默认居左 垂直均为居中
     * @param bold     是否加粗
     * @return
     */
    private HSSFCellStyle getStyle(int fontSize, HorizontalAlignment align, boolean bold, boolean border) {
        HSSFFont font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) fontSize);// 字体大小
        if (bold) {
            font.setBold(true);
        }
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFont(font);                         //设置字体
        style.setAlignment(align);          // 左右居中2 居右3 默认居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中1
        if (border) {
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            style.setLocked(true);
        }
        return style;
    }

    /**
     * 根据数据集生成Excel，并返回Excel文件流
     *
     * @param data      数据集
     * @param sheetName Excel中sheet单元名称
     * @param headNames 列表头名称数组
     * @return
     * @throws IOException
     */
    public InputStream getExcelFile(String ztName, String pzrq, List<KmyebDto> data, String sheetName, String[] headNames,
                                    int colWidths[]) throws IOException {
        workbook = new HSSFWorkbook();
        sheet = workbook.createSheet(sheetName);
        int startRow = 0;
        int page = 1;

        HSSFCellStyle cellStyle1 = getStyle(11, HorizontalAlignment.LEFT, false, true);
        HSSFCellStyle cellStyle2 = getStyle(11, HorizontalAlignment.CENTER, false, true);
        HSSFCellStyle cellStyle3 = getStyle(11, HorizontalAlignment.RIGHT, false, true);

        // 创建表头 startRow代表表体开始的行
        startRow = createHeadCell(ztName, pzrq, page++, headNames, colWidths, startRow);
        // 创建表体数据
        setCellData(data, cellStyle1, cellStyle2, cellStyle3, startRow);


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        workbook.write(baos);
        byte[] ba = baos.toByteArray();
        ByteArrayInputStream bais = new ByteArrayInputStream(ba);
        return bais;
    }


    /**
     * 创建表头
     *
     * @param headNames
     * @param colWidths
     */
    private int createHeadCell(String ztName, String pzrq, int page, String[] headNames, int colWidths[], int startRow) {
        // 表头标题
        HSSFCellStyle titleStyle = getStyle(22, HorizontalAlignment.CENTER, true, false);//样式
        createRow(titleStyle, 0x500, "科目余额表", startRow, startRow, 0, headNames.length - 1);
        //第二行
        HSSFRow row = sheet.createRow(startRow + 1);
        sheet.addMergedRegion(new CellRangeAddress(startRow + 1, startRow + 1, 0, 2));
        row.setHeight((short) 0x190);

//        //第二行右边部分
//        HSSFCell cell1_3 = row.createCell(headNames.length);
//        cell1_3.setCellStyle(getStyle(11, HorizontalAlignment.RIGHT, false, false));
//        cell1_3.setCellValue("第" + page + "页");

        //第三行左边部分
        sheet.addMergedRegion(new CellRangeAddress(startRow + 2, startRow + 2, 0, 2));
        HSSFRow row3 = sheet.createRow(startRow + 2);
        row3.setHeight((short) 0x190);
        HSSFCell cell = row3.createCell(0);
        cell.setCellStyle(getStyle(11, HorizontalAlignment.LEFT, false, false));
        cell.setCellValue("账套:" + ztName);

        //第三行中间部分
        sheet.addMergedRegion(new CellRangeAddress(startRow + 2, startRow + 2, 5, 8));
        HSSFCell cell2 = row3.createCell(5);
        cell2.setCellStyle(getStyle(11, HorizontalAlignment.CENTER, false, false));
        cell2.setCellValue(pzrq);

        //第三行右边部分
        HSSFCell cell3 = row3.createCell(headNames.length - 1);
        cell3.setCellStyle(getStyle(11, HorizontalAlignment.RIGHT, false, false));
        cell3.setCellValue("单位: 元");

        //第四行表头
        boolean b = (headNames != null && headNames.length > 0);
        if (b) {
            HSSFRow row2 = sheet.createRow(startRow + 3);
            row2.setHeight((short) 0x170);
            HSSFCell fcell = null;

            HSSFCellStyle cellStyle = getStyle(11, HorizontalAlignment.CENTER, false, true);

            for (int i = 0; i < headNames.length; i++) {
                fcell = row2.createCell(i);

                fcell.setCellStyle(cellStyle);
                fcell.setCellValue(headNames[i]);
                if (colWidths != null && i < colWidths.length) {
                    sheet.setColumnWidth(i, 32 * colWidths[i]);
                }
            }
        }
        return b ? startRow + 4 : startRow + 3;  //从哪一行开始渲染表体
    }

    /**
     * 创建表体数据
     *
     * @param data     表体数据
     * @param startRow 开始行
     */
    private void setCellData(List<KmyebDto> data, HSSFCellStyle cellStyle1, HSSFCellStyle cellStyle2, HSSFCellStyle cellStyle3, int startRow) {
        // 创建数据
        HSSFRow row = null;
        HSSFCell cell = null;
        int i = startRow;

        if (data != null && data.size() > 0) {
            DecimalFormat df = new DecimalFormat("##,##0.00");
            for (KmyebDto rowData : data) {
                row = sheet.createRow(i);
                row.setHeight((short) 0x170);

                int index = 0;
                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle1);
                cell.setCellType(CellType.STRING);
                cell.setCellValue(rowData.getKjkmCode());

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle1);
                cell.setCellType(CellType.STRING);
                cell.setCellValue(rowData.getKjkmName());

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle2);
                cell.setCellType(CellType.STRING);
                cell.setCellValue(rowData.getYefx1Str());

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String ncye = df.format(rowData.getNcye() == null ? 0 : rowData.getNcye());
                cell.setCellValue(ncye);


                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle2);
                cell.setCellType(CellType.STRING);
                cell.setCellValue(rowData.getYefx2Str());

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String qcye = df.format(rowData.getQcye() == null ? 0 : rowData.getQcye());
                cell.setCellValue(qcye);

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String jfs = df.format(rowData.getJfs() == null ? 0 : rowData.getJfs());
                cell.setCellValue(jfs);

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String dfs = df.format(rowData.getDfs() == null ? 0 : rowData.getDfs());
                cell.setCellValue(dfs);

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String jlj = df.format(rowData.getJlj() == null ? 0 : rowData.getJlj());
                cell.setCellValue(jlj);

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String dlj = df.format(rowData.getDlj() == null ? 0 : rowData.getDlj());
                cell.setCellValue(dlj);

                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle2);
                cell.setCellType(CellType.STRING);
                cell.setCellValue(rowData.getYefx3Str());


                cell = row.createCell(index++);
                cell.setCellStyle(cellStyle3);
                cell.setCellType(CellType.NUMERIC);
                String ye = df.format(rowData.getYe() == null ? 0 : rowData.getYe());
                cell.setCellValue(ye);
                i++;
            }
        }
    }

}
